package answer

import "sort"

type Data struct {
	Num  int
	Char byte
}

func frequencySort(s string) string {
	tempMap := make(map[byte]int)
	for _, b := range []byte(s) {
		tempMap[b]++
	}
	datas := make([]Data, 0)
	for key, value := range tempMap {
		datas = append(datas, Data{
			Num:  value,
			Char: key,
		})
	}
	sort.Slice(datas, func(i, j int) bool {
		return datas[i].Num > datas[j].Num
	})
	result := make([]byte, 0)
	for _, data := range datas {
		for i := 0; i < data.Num; i++ {
			result = append(result, data.Char)
		}
	}
	return string(result)
}
